import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _find from "lodash/find";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _class, _class2;

import React, { PureComponent } from 'react';
import { Icon, Menu, Spin, Dropdown, Avatar } from 'antd';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Bind } from 'lodash-decorators';
import NProgress from 'components/NProgress';
import { getCurrentRole } from 'utils/utils';
import { cleanMenuTabs } from 'utils/menuTab';
import defaultUserAvatar from 'cpaas-front/lib/assets/logo-usercenter-default.png';
import styles from "./index.less?modules";
var RoleSelect = (_dec = connect(function (_ref) {
  var _ref$user = _ref.user,
      user = _ref$user === void 0 ? {} : _ref$user;
  return {
    user: user
  };
}), _dec2 = Bind(), _dec3 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(RoleSelect, _PureComponent);

  var _super = _createSuper(RoleSelect);

  function RoleSelect() {
    var _this;

    _classCallCheck(this, RoleSelect);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      userAvatar: defaultUserAvatar
    };
    return _this;
  }

  _createClass(RoleSelect, [{
    key: "onClick",
    value: function onClick(_ref2) {
      var key = _ref2.key;
      var _this$props = this.props,
          _this$props$user = _this$props.user,
          user = _this$props$user === void 0 ? {} : _this$props$user,
          dispatch = _this$props.dispatch;
      var _user$roleList = user.roleList,
          roleList = _user$roleList === void 0 ? [] : _user$roleList;
      var newDefaultRole = _find(roleList, function (o) {
        return o.id === Number(key);
      }) || {};
      NProgress.start();
      var id = newDefaultRole.id,
          name = newDefaultRole.name;
      dispatch({
        type: 'user/updateCurrentRole',
        payload: {
          roleId: id
        }
      }).then(function (res) {
        NProgress.done();

        if (!(res && res.failed)) {
          dispatch({
            type: 'user/updateCurrentUser',
            payload: {
              currentRoleId: id,
              currentRoleName: name
            }
          }); // 切换到 工作台
          // warn 清空 tabs 信息

          cleanMenuTabs();
          dispatch(routerRedux.push({
            pathname: '/workplace'
          }));
          window.location.reload();
        }
      });
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      // 由于是再个人中心数据加载完成后再加载的数据, 所以需要先设置 头像
      var _this$props$user$curr = this.props.user.currentUser;
      _this$props$user$curr = _this$props$user$curr === void 0 ? {} : _this$props$user$curr;
      var userAvatar = _this$props$user$curr.imageUrl;

      if (userAvatar) {
        var img = new Image();
        img.onload = this.updateUserAvatar;
        img.onerror = this.setDefaultUserAvatar;
        img.src = userAvatar;
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _prevProps$user$curre = prevProps.user.currentUser;
      _prevProps$user$curre = _prevProps$user$curre === void 0 ? {} : _prevProps$user$curre;
      var prevUserAvatar = _prevProps$user$curre.imageUrl;
      var _this$props$user$curr2 = this.props.user.currentUser;
      _this$props$user$curr2 = _this$props$user$curr2 === void 0 ? {} : _this$props$user$curr2;
      var nextUserAvatar = _this$props$user$curr2.imageUrl;

      if (prevUserAvatar !== nextUserAvatar) {
        // 只有当 用户头像存在 才会设置 用户头像
        if (nextUserAvatar) {
          var img = new Image();
          img.onload = this.updateUserAvatar;
          img.onerror = this.setDefaultUserAvatar;
          img.src = nextUserAvatar;
        }
      }
    }
  }, {
    key: "updateUserAvatar",
    value: function updateUserAvatar() {
      var _this$props$user$curr3 = this.props.user.currentUser;
      _this$props$user$curr3 = _this$props$user$curr3 === void 0 ? {} : _this$props$user$curr3;
      var nextUserAvatar = _this$props$user$curr3.imageUrl;
      this.setState({
        userAvatar: nextUserAvatar
      });
    }
  }, {
    key: "setDefaultUserAvatar",
    value: function setDefaultUserAvatar() {
      this.setState({
        userAvatar: defaultUserAvatar
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props$user2 = this.props.user,
          user = _this$props$user2 === void 0 ? {} : _this$props$user2;
      var userAvatar = this.state.userAvatar;
      var _user$roleList2 = user.roleList,
          roleList = _user$roleList2 === void 0 ? [] : _user$roleList2;
      var menu = /*#__PURE__*/React.createElement(Menu, {
        selectedKeys: ["".concat((getCurrentRole() || {}).id)],
        onClick: this.onClick.bind(this)
      }, _isEmpty(roleList) ? /*#__PURE__*/React.createElement(Spin, null) : roleList.map(function (n) {
        return /*#__PURE__*/React.createElement(Menu.Item, {
          key: n.id
        }, n.name);
      }));
      return /*#__PURE__*/React.createElement(Dropdown, {
        overlay: menu,
        placement: "bottomLeft",
        trigger: ['click']
      }, /*#__PURE__*/React.createElement("span", {
        className: styles['select-role']
      }, /*#__PURE__*/React.createElement(Avatar, {
        size: "small",
        className: styles.avatar,
        src: userAvatar
      }), getCurrentRole().name || '', /*#__PURE__*/React.createElement(Icon, {
        type: "down"
      })));
    }
  }]);

  return RoleSelect;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "updateUserAvatar", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "updateUserAvatar"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "setDefaultUserAvatar", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "setDefaultUserAvatar"), _class2.prototype)), _class2)) || _class);
export { RoleSelect as default };